/*
 * @Description: 车辆信息（新增车辆信息）     NewVehicleInformation
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-27 15:04:24
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-08-20 10:35:03
 */
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/toast.dart';
import 'package:C20Driver/widget/rain_cell.dart';
import 'package:provider/provider.dart';
import 'package:C20Driver/provider/driver_information.dart';

class NewVehicleInformation extends StatefulWidget {
  @override
  _NewVehicleInformationState createState() => _NewVehicleInformationState();
}

class _NewVehicleInformationState extends State<NewVehicleInformation> {
  String _licensePlateNumber; // 车牌信息
  String _brand; // 车辆品牌车型
  String _certifyDate = '1990-01-01'; // 上牌时间
  String _vehicleColor = '白色'; // 车身颜色
  String _address = '选择'; // 城市
  String _vin; // 车辆识别码
  String _engineid; // 发动机号
  String _distance; // 行驶里程

  @override
  void initState() {
    super.initState();
  }

  @override
  didChangeDependencies() {
    super.didChangeDependencies();
    DriverInformation driverInformation =
        Provider.of<DriverInformation>(context, listen: false);
    setState(() {
      _licensePlateNumber =
          driverInformation.driverInfo.vehicleNumberPlate; // 车牌信息
      _brand = driverInformation.driverInfo.brandModels; // 车辆品牌车型
      _certifyDate = driverInformation.driverInfo.certifyDate; // 上牌时间
      _vehicleColor = driverInformation.driverInfo.vehicleColor; // 车身颜色
      _address = driverInformation.driverInfo.carAddress; // 城市
      _vin = driverInformation.driverInfo.vin; // 车辆识别码
      _engineid = driverInformation.driverInfo.engineid; // 发动机号
      _distance = driverInformation.driverInfo.distance; // 行驶里程
    });
  }

  // 设置上牌时间
  setCertifyDateA(context) {
    DateTime now = new DateTime.now();
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1900, 1, 1),
        maxTime: DateTime(now.year, now.month, now.day), onConfirm: (date) {
      setState(() {
        _certifyDate = '${date.year}-${date.month}-${date.day}';
      });
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  // String licensePlateNumber; // 车牌信息
  // String brand; // 车辆品牌车型
  // String certifyDateA = '1990-01-01'; // 上牌时间
  // String vehicleColor = '白色'; // 车身颜色
  // String address = '选择'; // 城市
  // String vin; // 车辆识别码
  // String engineid; // 发动机号
  // String distance; // 行驶里程
  _updataCarInfo(context, driverInformation) {
    if (_licensePlateNumber == null || _licensePlateNumber == '')
      return ToastX.center('请填写车牌信息');
    if (_brand == null || _brand == '') return ToastX.center('请填写品牌车型');
    if (_vehicleColor == null || _vehicleColor == '')
      return ToastX.center('请填写车身颜色');
    if (_address == null || _address == '选择') return ToastX.center('请选择城市');
    if (_vin == null || _vin == '') return ToastX.center('请填写车辆识别码');
    if (_engineid == null || _engineid == '') return ToastX.center('请填写发动机号');
    if (_distance == null || _distance == '') return ToastX.center('请填写行驶里程');
    Request.updataCarInfo(context, _licensePlateNumber, _brand, _vehicleColor,
        _vin, _engineid, _distance, _certifyDate, _address);
  }

  @override
  Widget build(BuildContext context) {
    DriverInformation provider =
        Provider.of<DriverInformation>(context, listen: false);
    return Scaffold(
      appBar: AppBar(
        title: Text('车辆信息'),
      ),
      backgroundColor: Color.fromARGB(255, 243, 244, 246),
      body: ListView(
        padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(30.0),
          right: ScreenUtil().setWidth(30.0),
        ),
        children: <Widget>[
          RainCell(
            title: "车辆牌号",
            des: _licensePlateNumber,
            onChanged: (res) {
              setState(() {
                _licensePlateNumber = res;
              });
            },
          ),
          RainCell(
            title: "品牌车型",
            des: _brand,
            onChanged: (res) {
              setState(() {
                _brand = res;
              });
            },
          ),
          RainCell(
            title: "上牌时间",
            des: _certifyDate,
            enabled: false,
            click: () => setCertifyDateA(context),
          ),
          RainCell(
            title: "车辆颜色",
            des: _vehicleColor,
            onChanged: (res) {
              setState(() {
                _vehicleColor = res;
              });
            },
          ),
          RainCell(
            title: "选择城市",
            des: _address,
            enabled: false,
            click: () =>
                NavigatorUtil.jump(context, '/urbanChoice').then((value) async {
              setState(() {
                _address = value;
              });
            }),
          ),
          RainCell(
            title: "车辆识别码",
            des: _vin,
            onChanged: (res) {
              setState(() {
                _vin = res;
              });
            },
          ),
          RainCell(
            title: "发动机号",
            des: _engineid,
            onChanged: (res) {
              setState(() {
                _engineid = res;
              });
            },
          ),
          RainCell(
            title: "行驶里程",
            des: _distance,
            onChanged: (res) {
              setState(() {
                _distance = res;
              });
            },
          ),
          SizedBox(height: ScreenUtil().setWidth(30.0)),
          Container(
            width: ScreenUtil().setWidth(700.0),
            padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
            child: MaterialButton(
              child: Text('提交'),
              color: Color.fromARGB(255, 255, 153, 0),
              minWidth: ScreenUtil().setWidth(700.0),
              disabledColor: Color.fromARGB(255, 188, 188, 188),
              height: ScreenUtil().setWidth(100.0),
              textColor: Colors.white,
              onPressed: () => _updataCarInfo(context, provider),
            ),
          )
        ],
      ),
    );
  }
}
